﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Dzzzr;

namespace Dzzzr
{
	public class DzzzrTeamManagerService : IDzzzrTeamManagerService
	{
		public User GetUser(string login, string password)
		{
			var q = (from user in DBDataContext.DataContext.Users
					 where user.Login == login && user.Password == password
					 select user);
			return q.FirstOrDefault<User>();
		}

		public Team GetUserTeam(User user)
		{
			var q = from team in DBDataContext.DataContext.Teams
					join usr in DBDataContext.DataContext.Users on team.ID equals usr.TeamID
					where team.ID == user.TeamID && usr.Login == user.Login && usr.Password == user.Password
					select team;
			return q.FirstOrDefault<Team>();
		}


		public User LoginUser(string login, string password)
		{
			var q = (from user in DBDataContext.DataContext.Users
					 where user.Login == login && user.Password == password
					 select user).ToList<User, SerializableEntity<User>>();
			if (q.Count == 0)
				return null;
			else
				return q[0].Entity;
		}

		public Team GetUserTeam2(User user)
		{
			var q = from team in DBDataContext.DataContext.Teams
					join usr in DBDataContext.DataContext.Users on team.ID equals usr.TeamID
					where team.ID == user.TeamID && usr.Login == user.Login && usr.Password == user.Password
					select team;
			return q.FirstOrDefault<Team, SerializableEntity<Team>>().Entity;
		}


		/// <summary>
		/// Login and get logged User
		/// </summary>
		/// <param name="name"></param>
		/// <param name="password"></param>
		/// <returns></returns>
		public DzzzrUser Login(string login, string password)
		{
			// get user from db
			var q = (from user in DBDataContext.DataContext.Users
					 where user.Login == login && user.Password == password
					 select user);
			//var q = (from user in DBDataContext.DataContext.Users
			//         //join team in DBDataContext.DataContext.Teams on user.TeamID equals team.ID
			//         join utr in DBDataContext.DataContext.UserToRoles on user.ID equals utr.UserID
			//         join ur in DBDataContext.DataContext.UserRoles on utr.UserRoleID equals ur.ID
			//         where user.Login == login && user.Password == password
			//         select new User {UserToRoles = utr });
			List<User> users = q.ToList<User>();
			if (users.Count() == 1)
				return CreateDzzzrUsersFromUsers(users)[0];
			return null;
		}

		/// <summary>
		/// Generate DzzzrUser by User
		/// </summary>
		/// <param name="users"></param>
		/// <returns></returns>
		private List<DzzzrUser> CreateDzzzrUsersFromUsers(List<User> users)
		{
			List<DzzzrUser> dzzzrUsers = new List<DzzzrUser>(users.Count);
			foreach(User user in users)
			{
				// create DzzzrUser
				DzzzrUser du = new DzzzrUser 
				{
					ID = user.ID,
					Login = user.Login,
					Password = user.Password,
					TeamID = user.TeamID 
				};

				// fill child properties
				foreach (UserToRole utr in user.UserToRoles)
				{
					du.UserToRoles.Add(
						new DzzzrUserToRole 
						{ 
							ID = utr.ID, 
							UserRole = new DzzzrUserRole 
							{ 
								ID = utr.UserRole.ID, Name = utr.UserRole.Name 
							}
						});
				}
				dzzzrUsers.Add(du);
			}
			return dzzzrUsers;
		}


		/// <summary>
		/// Get Team attached to User
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public DzzzrTeam GetTeam(DzzzrUser user)
		{
			var q = from team in DBDataContext.DataContext.Teams
					join usr in DBDataContext.DataContext.Users on team.ID equals usr.TeamID
					where team.ID == user.TeamID && usr.Login == user.Login && usr.Password == user.Password
					select team;
			List<Team> query = q.ToList<Team>();
			if (query.Count() == 1)
			{
				Team team = query[0];
				DzzzrTeam dt = new DzzzrTeam { 
					ID = team.ID, 
					Name = team.Name, 
					Description = team.Description,
					Users = CreateDzzzrUsersFromUsers(team.Users.ToList<User>())
				};
				return dt;
			}
			return null;
		}
	}
}
